package com.dmall.opening.up.restful.application;

import com.dmall.opening.up.records.exception.BizException;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.server.ResponseStatusException;

import java.util.HashMap;
import java.util.Map;

@ControllerAdvice
public class ExceptionHandler {

    @org.springframework.web.bind.annotation.ExceptionHandler(value = RuntimeException.class)
    public ResponseEntity<Map<String, Object>> runtimeException(RuntimeException e) {
        Map<String, Object> entity = new HashMap<>();
        entity.put("code", -100);
        entity.put("message", "服务器忙");
        return ResponseEntity.ok().body(entity);
    }

    @org.springframework.web.bind.annotation.ExceptionHandler(value = ResponseStatusException.class)
    public ResponseEntity<Map<String, Object>> httpStatusCodeException(ResponseStatusException e) {
        Map<String, Object> entity = new HashMap<>();
        entity.put("code", -200);
        entity.put("message", e.getMessage());
        return ResponseEntity.ok().body(entity);
    }

    @org.springframework.web.bind.annotation.ExceptionHandler(value = BizException.class)
    public ResponseEntity<Map<String, Object>> bizException(BizException e) {
        Map<String, Object> entity = new HashMap<>();
        entity.put("code", -300);
        entity.put("message", e.getMessage());
        return ResponseEntity.ok().body(entity);
    }
}
